package com.java.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import com.java.dao.CommentDao;
import com.java.entity.Comment;
import com.java.ext.MyButton;
import com.java.model.CommentTableModel;

/**
* @ClassName: DoctorCommentWindow
* @Description: 医生留言管理
* @author ChenJiaLu
* @date 2023年12月29日 下午2:57:24
*
 */
public class DoctorCommentWindow extends BaseJFrame {
	private static final long serialVersionUID = 1L;

	private List<Comment> comments = new ArrayList<Comment>();

	private Comment selectObj = null;

	private JTable table = null;

	public DoctorCommentWindow() {
		setTitle("医院就诊管理系统——医生（查看留言）");
		setSize(700, 480);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		initMenu(this);

		CommentDao dDao = new CommentDao();
		comments = dDao.getDoctorComments();

		CommentTableModel md = new CommentTableModel(comments);
		table = new JTable(md);
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);

		// 创建输入框和按钮
		JPanel inputPanel = new JPanel();
		inputPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		final MyButton replyButton = new MyButton("回复").warning();
		inputPanel.add(replyButton);
		add(inputPanel, BorderLayout.SOUTH);
		replyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (null == selectObj) {
					error("请选中");
					return;
				}
				String message = "请输入回复";
	            String title = "留言回复";
	            String inputValue = JOptionPane.showInputDialog(null, message, title, JOptionPane.QUESTION_MESSAGE);
	            if (null != inputValue) {
	            	CommentDao dDao = new CommentDao();
	            	selectObj.setReply(inputValue);
	            	dDao.updateComment(selectObj);
	            	CommentDao sDao = new CommentDao();
	            	comments = sDao.getDoctorComments();
					table.setModel(new CommentTableModel(comments));
	            	ok("回复成功");
	            }
			}
		});
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					int row = table.rowAtPoint(e.getPoint());
					selectObj = getObj(row);
					replyButton.setEnabled(true);
				}
			}
		});

		setVisible(true);
	}

	public Comment getObj(int index) {
		for (int i = 0; i < comments.size(); i++) {
			if (index == i) {
				return comments.get(i);
			}
		}
		return null;
	}

	public static void main(String[] args) {
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				public void run() {
					new DoctorCommentWindow();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}